package com.chenxin.transaction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication(exclude = MongoAutoConfiguration.class)
public class LisiTxmsgApplication {
    public static void main(String[] args) {
        SpringApplication.run(LisiTxmsgApplication.class);
    }
}


@Component      //1.注册组件
@EnableScheduling   // 2.开启定时任务
class HttpTask {
    // cron 格式：[秒][分][小时][日][月][周][年]
    // 这里是每20秒执行一次
    @Scheduled(cron = "* * * * * ?")
    private void requestTasks() {
        System.err.println("执行定时任务的时间: " + LocalDateTime.now());
        // Rest 服务的客户端库
        RestTemplate restTemplate = new RestTemplate();
        // 请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        // 添加请求头信息
        httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("userName", "a11");
        params.put("nickName", "111");
        // 请求实体，加入请求参数和请求头
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(params, httpHeaders);
        // 响应实体
        ResponseEntity<String> resp = null;
        // 无参数get请求
//        HttpEntity<Map<String, Object>> requestEntity = null;
//        resp = restTemplate.exchange("http://127.0.0.1:8080/", HttpMethod.GET, requestEntity, String.class);
        // 有参数post请求
        // 请求数据
        resp = restTemplate.getForEntity("https://blog.csdn.net/qq_31821733/article/details/124827901?spm=1001.2014.3001.5501", null);
        // 获取body数据
//        String body = resp.getBody();
        // 转成map
//        Map<String, Object> res = JSON.parseObject(body, new TypeReference< Map<String,Object>>() {
//        });
//        System.out.println(body);
    }
}

